package com.suoluo.zenadmin.controllers.admin.trip;

import java.util.List;

import javax.annotation.Resource;

import net.paoding.rose.web.Invocation;
import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;

import com.suoluo.zenadmin.common.CommonResp;
import com.suoluo.zenadmin.common.JSONPageData;
import com.suoluo.zenadmin.common.PageUtil;
import com.suoluo.zenadmin.common.config.SystemConfigHolder;
import com.suoluo.zenadmin.controllers.JSONInterceptor.JSON;
import com.suoluo.zenadmin.service.UpgradeService;
import com.suoluo.zenadmin.service.trip.TripService;
import com.suoluo.zenadmin.vo.PageVO;
import com.suoluo.zenadmin.vo.trip.TripLocationVO;
import com.suoluo.zenadmin.vo.trip.TripVO;
import com.suoluo.zenadmin.vo.trip.TripdestVO;

@Path("/")
public class TripDestController {
	
	@Resource(name = "tripService")
	private TripService tripService;
	@Resource(name="upgradeService")
	private UpgradeService upgradeService;
	
	/**
	 * 标记有未完成的行程
	 */
	private static final int IS_TRUE = 1;
	/**
	 * 标记没有未完成的行程
	 */
	private static final int IS_FALSE = 0;
	
	/********************行程管理**start************************/
	@Get("vlist")
	public String toListTrip(Invocation invo)
	{
		return "listTrip";
	}
	@JSON
	@Post("list")
	public Object showTripList(Invocation inv,PageVO page)
	{
		int currpage = page.getPage();
		int rows = page.getRows();
		int start = PageUtil.calcStartRow(currpage, rows);
		//查询已完成的行程信息
		List<TripVO> list = tripService.findListTrip(start,rows);
		//统计已创建完成的行程信息
		int total = tripService.findTripCount();
		JSONPageData data = new JSONPageData();
		data.setRows(list);
		data.setTotal(total);
		return data;
	}
	@Get("vupdate")
	public String toUpdateTrip(Invocation inv,@Param("tripId")int tripId) {
		TripVO trip = tripService.findTripById(tripId);
		inv.addModel("trip",trip);
		return "updateTrip";
	}
	@JSON
	@Post("update")
	public Object updateTrip(TripVO trip) {
		tripService.updateTrip(trip);
		return CommonResp.SUCCESS;
	}
	/********************行程管理**end************************/

	/********************行程目的地管理**start************************/
	@Get("tripdest/vlist")
	public String showListTripDest() {

		return "listTripDests";
	}
	@JSON
	@Post("tripdest/list")
	public Object listTripdests(PageVO page,@Param("destName")String destName) {
		JSONPageData data = new JSONPageData();
		
		int currPage = page.getPage();
		int rows = page.getRows();
		int start = PageUtil.calcStartRow(currPage, rows);
		//分页查询数据
		List<TripdestVO> list = tripService.findListTripdest(start,rows,destName);
		data.setRows(list);
		//统计总数
		int total = tripService.findTripdestCount(destName);
		data.setTotal(total);
		return data;
	}
	
	@JSON
	@Post("syndests")
	public Object synDests() {
		int res = tripService.updateSyncRemainSysDests();
		CommonResp resp = new CommonResp();
		resp.setResultCode(CommonResp.SUCCESS_CODE);
		if(res == 0)
		{
			resp.setResultMsg("数据已是最新，不用重复同步了！");
		}
		else
		{
			resp.setResultMsg("成功同步了"+res+"条数据！");
		}
		return resp;
	}
	
	
	@Get("tripdest/vupdate")
	public String toUpdateTripdest(Invocation inv,@Param("tripdestId")int tripdestId){
		TripdestVO tripdest = tripService.findTripdestById(tripdestId);
		inv.addModel("tripdest",tripdest);
		return "updateTripdest";
	}
	@JSON
	@Post("tripdest/update")
	public Object updateTripdest(TripdestVO tripdest){
		tripService.updateTripdest(tripdest);
		return CommonResp.SUCCESS;
	}
	/********************行程目的地管理**end************************/
	
	/********************行程 目的地下地点管理**start************************/
	@Get("triplocation/vlist")
	public String showListTripLocation(Invocation inv,@Param("tripdestId")int tripdestId) {
		TripdestVO tripdest = tripService.findTripdestById(tripdestId);
		inv.addModel("tripdest",tripdest);
		return "listTripLocation";
	}
	@JSON
	@Post("triplocation/list")
	public Object listTripLocation(@Param("tripdestId")int tripdestId,@Param("locationName")String locationName,PageVO page) {
		JSONPageData data = new JSONPageData();
		//计算分页信息
		int currPage = page.getPage();
		int rows = page.getRows();
		int start = PageUtil.calcStartRow(currPage, rows);
		//查询行程目的地下的地点信息
		List<TripLocationVO> list =  tripService.findListTripLocation(tripdestId,start,rows,locationName);
		data.setRows(list);
		//统计行程地点总数
		int count = tripService.findTripLocationCount(tripdestId,locationName);
		data.setTotal(count);
		return data;
	}
	
	@JSON
	@Post("synlocation")
	public Object synTripLocation(TripdestVO tripdest) {
		int res = tripService.updateSyncTripdestLocations(tripdest);
		CommonResp resp = new CommonResp();
		resp.setResultCode(CommonResp.SUCCESS_CODE);
		if(res == 0)
		{
			resp.setResultMsg("数据已是最新，不用重复同步了！");
		}
		else
		{
			resp.setResultMsg("成功同步了"+res+"条数据！");
		}
		return resp;
	}
	
	@JSON
	@Post("syntraffic")
	public Object synTraffic(TripdestVO tripdest) {
		int res = tripService.updateSyncTripdestTraffic(tripdest);
		CommonResp resp = new CommonResp();
		resp.setResultCode(CommonResp.SUCCESS_CODE);
		if(res == 0)
		{
			resp.setResultMsg("数据已是最新，不用重复同步了！");
		}
		else
		{
			resp.setResultMsg("成功同步了"+res+"条数据！");
		}
		
		return resp;
	}
	
	@Get("triplocation/vupdate")
	public String toUpdateTripLocation(Invocation inv,@Param("locationId")int locationId){
		TripLocationVO tripLocation = tripService.findTripLocationById(locationId);
		inv.addModel("tripLocation",tripLocation);
		return "updateTripLocation";
	}
	@JSON
	@Post("triplocation/update")
	public Object updateTripLocation(TripLocationVO tripLocation){
		tripService.updateTripLocation(tripLocation);
		return CommonResp.SUCCESS;
	}
	@JSON
	@Post("tripdests")
	public Object findListTripDests(){
		return tripService.findListTripDests();
	}
	/********************行程目的地下地点管理**end************************/
	
	/********************行程升级代码**start*********************/
	@Get("upgrade")
	public String toUpgradeTripView(){
		return "upgradeTripView";
	}

	/********************行程升级代码**end*********************/
	
	/***************公网Ip管理**start****************/
	@Get("vsaveip")
	public String toAddIPView(Invocation inv){
		String str = SystemConfigHolder.getTripPublicIP();
		inv.addModel("res",str);
		return "addPublicIP";
	}
	
	@Post("saveip")
	public String addIP(Invocation inv,@Param("ipstr")String ipstr) {
		SystemConfigHolder.setTripPublicIP(ipstr);
		String str = SystemConfigHolder.getTripPublicIP();
		inv.addModel("res",str);
		return "addPublicIP";
	}
	/***************公网Ip管理**end****************/
	
	/***************过期行程管理**start****************/
	@Get("expire/vlist")
	public String toListExpireTrip(){
		return "listExpireTrip";
	}
	@JSON
	@Post("expire/list")
	public Object listExpireTrip(PageVO page){
		JSONPageData data = new JSONPageData();
		List<TripVO> list = tripService.findListExpireTrip(page);
		data.setRows(list);
		int total = tripService.findExpireTripCount();
		data.setTotal(total);
		return data;
	}
	
	/***************过期行程管理**end****************/
}
